package com.fashion.web.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.PatternSyntaxException;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fashion.model.InLineWord;
import com.fashion.model.News;
import com.fashion.model.NewsColumn;
import com.fashion.model.brand.Brand;
import com.fashion.model.gallery.Album;
import com.fashion.service.ColumnService;
import com.fashion.service.InLineWordService;
import com.fashion.service.NewsService;
import com.fashion.service.brand.BrandService;
import com.fashion.service.gallery.AlbumService;
import com.fashion.utils.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
@Namespace("/brand")
public class BrandAction extends ActionSupport {
	private static final Logger logger = Logger.getLogger(BrandAction.class);

	private String district;
	private String industry;
	private NewsColumn industryColumn = null;
	private String firstChar;
	private List<NewsColumn> industries = new ArrayList<NewsColumn>();
	private List<NewsColumn> districts = new ArrayList<NewsColumn>();
	private String title;
	private List<Brand> brandsByFirstChar;
	private List<Brand> brandsByKeyword;
	private List<Brand> recommendBrands;
	@Resource(name = "brandService")
	private BrandService brandService;
	@Resource(name = "newsService")
	private NewsService newsService;
	@Resource(name = "columnService")
	private ColumnService columnService;
	@Resource(name = "albumService")
	private AlbumService albumService;
	@Resource(name = "inLineWordService")
	private InLineWordService inLineWordService;
	private Brand brand;
	private List<News> newsList;
	private List<Album> albumList;
	private List<Brand> brandList;
	private int pageNum = 1;
	private int pageSize = 30;
	private int pageCount;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "brand.jsp") })
	public String index() throws Exception {
		brandList = brandService.find(null, null, null, (pageNum - 1)*pageSize, pageSize);
		pageCount = brandService.count(null, null, null)/pageSize + 1;
		return SUCCESS;
	}

	@Action(value = "find_brands", results = { @Result(name = SUCCESS, location = "find_brands.jsp") })
	public String find() throws Exception {
		NewsColumn industryRootColumn = columnService.findColumnByUrl("brand", "brand");
		industries = industryRootColumn.getChildrenColumns();
		NewsColumn districtRootColumn = columnService.findColumnByUrl("district", "district");
		districts = districtRootColumn.getChildrenColumns();
		
		if (industry != null && !"all".equals(industry)) {
			industryColumn = columnService.findColumnByUrl("brand", industry);
		}
		NewsColumn districtColumn = null;
		if (district != null && !"all".equals(district)) {
			districtColumn = columnService.findColumnByUrl("district", district);
		}
		String fc = null;
		if (firstChar != null && !"all".equals(firstChar)) {
			fc = firstChar;
		}
		brandList = brandService.find(industryColumn, districtColumn, fc, (pageNum - 1)*pageSize, pageSize);
		pageCount = brandService.count(industryColumn, districtColumn, fc)/pageSize + 1;
		return SUCCESS;
	}

	@Action(value = "find_brand", results = { @Result(name = SUCCESS, location = "brand_detail.jsp") })
	public String findBrand() throws Exception {
		try {
			brand = brandService.findById(brand.getId());
			List<String> keywords = new ArrayList<String>();
			String cnTitle = brand.getCnTitle();
			if(cnTitle != null){
				cnTitle.replace(" ", "$");
			}
			if (cnTitle != null && !"".equals(cnTitle.trim())) {
				keywords.add(brand.getCnTitle());
			}
			String enTitle = brand.getEnTitle().replace(" ", "$");
			if (enTitle != null && !"".equals(enTitle.trim())) {
				keywords.add(enTitle);
			}
			newsList = newsService.searchTagNews(keywords, 1, 4);
			albumList = albumService.findByKeywords(keywords, 0, 3);
			brandList = brandService.find(0, 8, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Action(value = "find_brand_story", results = { @Result(name = SUCCESS, location = "brand_detail_story.jsp") })
	public String findStory() throws Exception {
		brand = brandService.findById(brand.getId());
		List<InLineWord> inLineWords = inLineWordService.searchInLineWords("", 1, 280);
		String introduction = brand.getIntroduction();
		for (InLineWord inLineWord : inLineWords) {
			try {
				introduction = StringUtils.addInline(introduction, inLineWord);
			} catch (PatternSyntaxException ex) {
				logger.error("PatternSyntaxException in showNews()-->addInline(), should fix it later");
			} catch (Exception e) {
				logger.error("Unexpected error in addInline", e);
			}
		}
		brand.setIntroduction(introduction);
		String additionalText = brand.getAdditionalText();
		for (InLineWord inLineWord : inLineWords) {
			try {
				additionalText = StringUtils.addInline(additionalText, inLineWord);
			} catch (PatternSyntaxException ex) {
				logger.error("PatternSyntaxException in showNews()-->addInline(), should fix it later");
			} catch (Exception e) {
				logger.error("Unexpected error in addInline", e);
			}
		}
		brand.setAdditionalText(additionalText);
		brandList = brandService.find(0, 8, null);
		return SUCCESS;
	}

	@Action(value = "find_brand_news", results = { @Result(name = SUCCESS, location = "brand_detail_news.jsp") })
	public String findNews() throws Exception {
		brand = brandService.findById(brand.getId());
		List<String> keywords = new ArrayList<String>();
		String cnTitle = brand.getCnTitle().replace(" ", "$");
		if (cnTitle != null && !"".equals(cnTitle.trim())) {
			keywords.add(brand.getCnTitle());
		}
		String enTitle = brand.getEnTitle().replace(" ", "$");
		if (enTitle != null && !"".equals(enTitle.trim())) {
			keywords.add(enTitle);
		}
		newsList = newsService.searchTagNews(keywords, 1, 10);
		brandList = brandService.find(0, 8, null);
		return SUCCESS;
	}

	@Action(value = "find_brand_gallery", results = { @Result(name = SUCCESS, location = "brand_detail_gallery.jsp") })
	public String findGallery() throws Exception {
		brand = brandService.findById(brand.getId());
		List<String> keywords = new ArrayList<String>();
		String cnTitle = brand.getCnTitle().replace(" ", "$");
		if (cnTitle != null && !"".equals(cnTitle.trim())) {
			keywords.add(brand.getCnTitle());
		}
		String enTitle = brand.getEnTitle().replace(" ", "$");
		if (enTitle != null && !"".equals(enTitle.trim())) {
			keywords.add(enTitle);
		}
		albumList = albumService.findByKeywords(keywords, 0, 20);
		brandList = brandService.find(0, 8, null);
		return SUCCESS;
	}

	public List<Brand> getBrandsByFirstChar() {
		return brandsByFirstChar;
	}

	public List<Brand> getBrandsByKeyword() {
		return brandsByKeyword;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setKeyword(String keyword) {
		this.industry = keyword;
	}

	public String getTitle() {
		return title;
	}

	public List<Brand> getRecommendBrands() {
		return recommendBrands;
	}

	public List<Album> getAlbumList() {
		return albumList;
	}

	public List<Brand> getBrandList() {
		return brandList;
	}

	public List<NewsColumn> getIndustries() {
		return industries;
	}

	public List<NewsColumn> getDistricts() {
		return districts;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	public String getIndustry() {
		return industry;
	}

	public void setIndustry(String industry) {
		this.industry = industry;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageCount() {
		return pageCount;
	}

	public String getFirstChar() {
		return firstChar;
	}

	public void setFirstChar(String firstChar) {
		this.firstChar = firstChar;
	}

	public NewsColumn getIndustryColumn() {
		return industryColumn;
	}
	
}
